#include"comm.hpp"

//服务器端读
int main()
{
    FIFO fifo(PATH);
    int rfd=open(PATH,O_RDONLY);
    if(rfd<0)
    {
        cerr<<"open fifo fail... errno: "<<errno<<",errstring: "<<strerror(errno)<<endl;
        return -1;
    }
    //下面这句当客户端连接上才会打印出来
    //当写端未打开，先读打开，open会阻塞，直到写端打开
    cout<<"open success..."<<endl;
    char buffer[1024];
    while(1)
    {
        int n=read(rfd,buffer,sizeof(buffer));//读取内容
        if(n<0)
        {
            cerr<<"read fifo fail... errno: "<<errno<<",errstring: "<<strerror(errno)<<endl;
            break;
        }
        if(n==0)
        {
            cout<<"client already quit...,me too!"<<endl;
            break;
        }
        else
        {
            buffer[n]=0;
            cout<<"client say: "<<buffer<<endl;
        }    
    }
    close(rfd);
    return 0;
}